package com.jlz.beans.def;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jlz.managers.def.ValueLabel;
import com.julewa.db.DB;
import com.julewa.db.DataType;
import com.julewa.db.Entity;
import com.julewa.db.SearchDefine;
import com.julewa.db.StringTrimmer;
import com.julewa.db.checkers.DataStateChecker;

/*************************
 * 专用于处理数值类型的数据
 *************************/
@DB.USECACHE
@Component @Scope(Entity.SCOPE)
public class ValuetypeBean extends Entity<Integer>{
	
	private @DB.COLUMN(min=2,max=16) @DB.AUTOTRIM 
		String code = "";
	private @DB.COLUMN(min=2,max=32) @DB.AUTOTRIM 
		String label = "";
	
	//
	private @DB.COLUMN(min=DataType.MIN, max=DataType.MAX) 
		int type = DataType.STRING.getValue();
	
	//range
	private @DB.COLUMN double min = 0;
	private @DB.COLUMN double max = 0;
	private @DB.COLUMN boolean checkMin = false;
	private @DB.COLUMN boolean checkMax = false;
	private @DB.COLUMN boolean nullable = true;
	//
	private @DB.COLUMN boolean lockedValueLabels = false;
	private @DB.COLUMN(max=256) String valueRange = "";
	private @DB.COLUMN(max=1024) String labelRange = "";
	
	//search
	private @DB.COLUMN boolean parsable = true;
	private @DB.COLUMN boolean storable = true;
	
	//autotrim
	private @DB.COLUMN boolean autotrim = true;
	
	//
	private @DB.DEPENDON("valueLabels") List<ValueLabel> valueLabels = null;
	private @DB.DEPENDON({"type", "autotrim"}) StringTrimmer autoTrimmer = null;
	private @DB.DEPENDON({"parsable", "storable"}) SearchDefine searchDefine = null;
	private @DB.DEPENDON("type") DataType dataType = null;
	private @DB.DEPENDON({"type", "nullable", "min", "max", "checkMin", "checkMax"}) DataStateChecker checker = null;
	//
	
	/////////////////////////////////
	public List<ValueLabel> getValueLabels() {
		if(this.valueLabels == null){
			valueLabels = new ArrayList<ValueLabel>();
			if(valueRange==null || "".equals(valueRange))return valueLabels;
			String vs[] = valueRange.split(",");
			String ls[] = labelRange.split(",");
			for(int i = 0; i < vs.length; ++ i){
				valueLabels.add(new ValueLabel(Double.parseDouble(vs[i]), ls[i]));
			}
		}
		return valueLabels;
	}
	
	public void addValueLabel(Double value, String label){
		getValueLabels().add(new ValueLabel(value, label));
	}
	
	public void clearValueLabels(){
		if(this.valueLabels == null){
			valueLabels = new ArrayList<ValueLabel>();
		}else{
			valueLabels.clear();
		}
	}
	
	public void resetValueLabels(){
		if(valueLabels == null)return;
		StringBuffer vrange = null;
		StringBuffer lrange = null;
		for(ValueLabel p: valueLabels){
			if(vrange == null){
				vrange = new StringBuffer(p.A.toString());
				lrange = new StringBuffer(p.B);
			}else{
				vrange.append(",").append(p.A.toString());
				lrange.append(",").append(p.B);
			}
		}
		if(vrange == null){
			this.valueRange = "";
			this.labelRange = "";
		}else{
			this.valueRange = vrange.toString();
			this.labelRange = lrange.toString();
		}
	}

	public StringTrimmer getAutoTrimmer() {
		if(this.autoTrimmer == null){
			if(!this.getDataType().isTextData())return null;
			this.autoTrimmer = new StringTrimmer();
		}
		return autoTrimmer;
	}

	public SearchDefine getSearchDefine() {
		if(searchDefine == null){
			searchDefine = new SearchDefine(
					this.getDataType().isTextData() && this.parsable, 
					this.storable);
		}
		return searchDefine;
	}
	
	public DataType getDataType() {
		if(dataType == null){
			dataType = DataType.of(this.type);
		}
		return dataType;
	}
	
	public DataStateChecker getDataStateChecker(){
		if(checker == null){
			checker = getDataType().generateChecker();
			checker.initialize(true, nullable, min, max);
		}
		return checker;
	}

	public Class<?> getTypeClass() {
		return getDataType().getObjectClass();
	}
	
	//////////////////////////////////////
	public String getValueRange() {
		return valueRange;
	}

	public void setValueRange(String valueRange) {
		this.valueRange = valueRange;
	}

	public String getLabelRange() {
		return labelRange;
	}

	public void setLabelRange(String labelRange) {
		this.labelRange = labelRange;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public double getMin() {
		return min;
	}

	public void setMin(double min) {
		this.min = min;
	}

	public double getMax() {
		return max;
	}

	public void setMax(double max) {
		this.max = max;
	}

	public boolean isCheckMin() {
		return checkMin;
	}

	public void setCheckMin(boolean checkMin) {
		this.checkMin = checkMin;
	}

	public boolean isCheckMax() {
		return checkMax;
	}

	public void setCheckMax(boolean checkMax) {
		this.checkMax = checkMax;
	}

	public boolean isLockedValueLabels() {
		return lockedValueLabels;
	}

	public void setLockedValueLabels(boolean lockedValueLabels) {
		this.lockedValueLabels = lockedValueLabels;
	}

	public boolean isParsable() {
		return parsable;
	}

	public void setParsable(boolean parsable) {
		this.parsable = parsable;
	}

	public boolean isStorable() {
		return storable;
	}

	public void setStorable(boolean storable) {
		this.storable = storable;
	}

	public boolean isAutotrim() {
		return autotrim;
	}

	public void setAutotrim(boolean autotrim) {
		this.autotrim = autotrim;
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}


}
